package org.pluslibs.framework.swingplus.frame;

import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import org.pluslibs.framework.swingplus.jdialog.JDialogError;
import org.pluslibs.framework.swingplus.utils.JMessageInterface;
import org.pluslibs.framework.swingplus.utils.SwingEventThreadUtils;

public class JFrameMessage extends JFrame implements JMessageInterface {
    private static final long serialVersionUID = 6590381737962537156L;

    public JFrameMessage() {
        super();
        super.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }

    public JFrameMessage(String title) {
        super(title);
        super.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }

    final public void beep() {
        Toolkit.getDefaultToolkit().beep();
    }

    final public void displayMessage(final String title, final String message) {
        if (SwingUtilities.isEventDispatchThread()) {
            JOptionPane.showMessageDialog(this, message, title, JOptionPane.INFORMATION_MESSAGE);
        } else {
            SwingEventThreadUtils.invokeAndWait_NoException(new Runnable() {
                public void run() {
                    JOptionPane.showMessageDialog(JFrameMessage.this, message, title, JOptionPane.INFORMATION_MESSAGE);
                }
            });
        }
    }

    final public void displayMessage(final String message) {
        if (SwingUtilities.isEventDispatchThread()) {
            JOptionPane.showMessageDialog(this, message, super.getTitle(), JOptionPane.INFORMATION_MESSAGE);
        } else {
            SwingEventThreadUtils.invokeAndWait_NoException(new Runnable() {
                public void run() {
                    JOptionPane.showMessageDialog(JFrameMessage.this, message, JFrameMessage.this.getTitle(), JOptionPane.INFORMATION_MESSAGE);
                }
            });
        }
    }

    final public void displayError(final String title, final String message) {
        if (SwingUtilities.isEventDispatchThread()) {
            JOptionPane.showMessageDialog(this, message, title, JOptionPane.ERROR_MESSAGE);
        } else {
            SwingEventThreadUtils.invokeAndWait_NoException(new Runnable() {
                public void run() {
                    JOptionPane.showMessageDialog(JFrameMessage.this, message, title, JOptionPane.ERROR_MESSAGE);
                }
            });
        }
    }

    final public void displayError(final String message) {
        if (SwingUtilities.isEventDispatchThread()) {
            JOptionPane.showMessageDialog(this, message, "Error", JOptionPane.ERROR_MESSAGE);
        } else {
            SwingEventThreadUtils.invokeAndWait_NoException(new Runnable() {
                public void run() {
                    JOptionPane.showMessageDialog(JFrameMessage.this, message, "Error", JOptionPane.ERROR_MESSAGE);
                }
            });
        }
    }

    final public void displayError(final Throwable e) {
        if (SwingUtilities.isEventDispatchThread()) {
            e.printStackTrace();
            new JDialogError(this, e);
        } else {
            SwingEventThreadUtils.invokeAndWait_NoException(new Runnable() {
                public void run() {
                    e.printStackTrace();
                    new JDialogError(JFrameMessage.this, e);
                }
            });
        }
    }
}
